package com.future.mayi.adapter;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.future.mayi.activity.BasePage;

import java.util.List;


/**
 * Created by 周大虎 on 2016/12/2.
 */

public class BasePageAdapter extends PagerAdapter {

    private List<BasePage> pageList;
    private String[] tabTitle;
    private List<String> tabTitles;


    public BasePageAdapter(List<BasePage> pageList) {
        this.pageList = pageList;
    }


    public BasePageAdapter(List<BasePage> pageList, String[] tabTitle) {
        this.pageList = pageList;
        this.tabTitle = tabTitle;
    }


    public BasePageAdapter(List<BasePage> pageList, List<String> tabTitle) {
        this.pageList = pageList;
        this.tabTitles = tabTitle;
    }

    @Override
    public int getCount() {
        return pageList.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(pageList.get(position).getRootView());
        return pageList.get(position).getRootView();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (tabTitle != null && position < tabTitle.length)
            return tabTitle[position];
        else if (tabTitles != null && position < tabTitles.size())
            return tabTitles.get(position);
        else
            return super.getPageTitle(position);
    }
}
